package com.pactera.madp.cp.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.entity.wm.WorkMsgConfigEntity;
import com.pactera.madp.cp.api.bo.CorpShowBO;
import com.pactera.madp.cp.api.dto.ReqCorpStoreDTO;
import com.pactera.madp.cp.api.dto.ReqStepUpdateDTO;
import com.pactera.madp.cp.api.bo.StepCreateBO;
import com.pactera.madp.cp.common.util.RSAUtils;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.TenantMapper;
import com.pactera.madp.cp.mapper.corp.CorpMapper;
import com.pactera.madp.cp.mapper.wm.WorkMsgConfigMapper;
import com.pactera.madp.cp.service.IChatConfigService;
import com.pactera.madp.cp.service.IWorkMsgConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author: yangpengwei
 * @time: 2020/12/4 12:09 下午
 * @description 会话内容存档配置 - 微信后台配置
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class ChatConfigServiceImpl extends ServiceImpl<WorkMsgConfigMapper,WorkMsgConfigEntity> implements IChatConfigService {

    @Autowired
    private IWorkMsgConfigService msgConfigService;
    @Autowired
    private CorpMapper corpMapper;
    @Autowired
    private TenantMapper tenantMapper;

    @Value("${mochat.serviceUrl}")
    private String serviceUrl;

    @Override
    public CorpShowBO getCorpShowInfo(int corpId) {
        CorpShowBO corpShowBO = new CorpShowBO();

        CorpEntity corpEntity = corpMapper.selectById(corpId);
        if (corpEntity == null) {
            throw new CheckedException("企业信息不存在");
        }

        corpShowBO.setName(corpEntity.getCorpName());
        corpShowBO.setSocialCode(corpEntity.getSocialCode());

        WorkMsgConfigEntity workMsgConfigEntity = msgConfigService.getByCorpId(corpId);

        corpShowBO.setId(workMsgConfigEntity.getId());
        corpShowBO.setChatAdmin(workMsgConfigEntity.getChatAdmin());
        corpShowBO.setChatAdminPhone(workMsgConfigEntity.getChatAdminPhone());
        corpShowBO.setChatAdminIdcard(workMsgConfigEntity.getChatAdminIdcard());
        corpShowBO.setChatApplyStatus(workMsgConfigEntity.getChatApplyStatus());
        List<String> whiteServerIps = JSONUtil.toList(JSONUtil.parseArray(workMsgConfigEntity.getWhiteServerIps()),String.class);
        corpShowBO.setWhiteServerIps(whiteServerIps);

        return corpShowBO;
    }


    @Override
    public StepCreateBO getStepInfo() {
        int corpId = CorpContextHolder.getCorpId();

        // 读取会话内容存档配置
        WorkMsgConfigEntity entity = msgConfigService.getByCorpId(corpId);
        StepCreateBO stepCreateBO = new StepCreateBO();
        stepCreateBO.setId(entity.getId());
        stepCreateBO.setChatApplyStatus(entity.getChatApplyStatus());

        int chatApplyStatus = entity.getChatApplyStatus();
        if (chatApplyStatus == 1) {
            //查询企业名称
            CorpEntity corpEntity = corpMapper.selectById(corpId);
            stepCreateBO.setCorpName(corpEntity.getCorpName());
            //stepCreateBO.setWxCorpId(corpEntity.getWxCorpId());
        }
        if (chatApplyStatus == 2) {
            // 填写企业信息 返回客服联系方式
            // 通过配置文件 yml 读取客服联系方式二维码
            stepCreateBO.setServiceContactUrl(serviceUrl);
        }
        if (chatApplyStatus == 3) {
            // 添加客户提交资料 客服后台修改
            // 读取租户信息, 暂时默认读取第一条租户信息
            /*TenantEntity tenantEntity = new TenantEntity();
            tenantEntity.setId(1);
            tenantEntity = tenantMapper.selectOne(new QueryWrapper<>(tenantEntity));

            stepCreateBO.setChatWhitelistIpJson(tenantEntity.getServerIps());*/
            //List<String> ips = JSONUtil.toList(JSONUtil.parseArray(entity.getWhiteServerIps()),String.class);
            //stepCreateBO.setWhiteServerIps(ips);

            // 公私钥
            Map<String, String> keys = RSAUtils.getRsaKeys();
            stepCreateBO.setRsaPrivateKey(keys.get(RSAUtils.KEY_PRIVATE));
            stepCreateBO.setRsaPublicKey(keys.get(RSAUtils.KEY_PUBLIC));
        }

        return stepCreateBO;
    }


    @Override
    public boolean setCorpStore(ReqCorpStoreDTO req) {
        Integer corpId = CorpContextHolder.getCorpId();
        if(corpId == null) {
            throw new CheckedException("企业id不能为空");
        }

        CorpEntity corpEntity = new CorpEntity();
        corpEntity.setCorpId(corpId);
        corpEntity = corpMapper.selectOne(new QueryWrapper<>(corpEntity));
        corpEntity.setSocialCode(req.getSocialCode());
        corpMapper.updateById(corpEntity);

        WorkMsgConfigEntity workMsgConfigEntity = msgConfigService.getByCorpId(corpId);
        workMsgConfigEntity.setChatAdmin(req.getChatAdmin());
        workMsgConfigEntity.setChatAdminPhone(req.getChatAdminPhone());
        workMsgConfigEntity.setChatAdminIdcard(req.getChatAdminIdcard());
        workMsgConfigEntity.setChatApplyStatus(2);

        return msgConfigService.updateById(workMsgConfigEntity);
    }


    @Override
    public boolean stepService(Integer id) {
        WorkMsgConfigEntity entity = new WorkMsgConfigEntity();
        entity.setId(id);
        entity.setChatApplyStatus(3);

        return msgConfigService.updateById(entity);
    }


    @Override
    public boolean stepConfig(ReqStepUpdateDTO dto) {
        // 会话内容存档配置存储
        WorkMsgConfigEntity entity = msgConfigService.getById(dto.getId());
        entity.setChatApplyStatus(4);
        entity.setChatRsaKey(dto.getChatRsaKey());
        entity.setChatSecret(dto.getChatSecret());
        entity.setChatStatus(dto.getChatStatus());
        entity.setWhiteServerIps(JSONUtil.toJsonStr(entity.getWhiteServerIps()));

        return msgConfigService.updateById(entity);
    }

}
